<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 17/4/12
 * Time: 17:43
 */
class Dictionary_model extends MYNTModel {
  
    function __construct() {
        parent::__construct();
    }

    public function get_dictionary_types($start, $length, $code, $orderby) {

        if (!empty($code)) {
            $this->db->like('code', $code);
        }

        return $this->getPageDataTables($start, $length, self::TABLE_DICTIONARY_TYPE, $orderby);
    }

    
    public function add_dictionary($code, $name) {
        $data = array(
            'code' => $code,
            'name' => $name,
            'status' => 1,
            'createtime'=> time()
         );

        $this->db->insert(self::TABLE_DICTIONARY_TYPE, $data);
        return $this->db->affected_rows() > 0;
    }

    public function update_dictionary_type_by_id($id, $code, $name) {
        $this->db ->set("code", $code)
            ->set("name",$name)
            -> where("id", $id)
            -> update(self::TABLE_DICTIONARY_TYPE);
        return $this->db->affected_rows() > 0;
    }

    public function get_dictionary_by_code($code) {
        $query = $this->db->get_where(self::TABLE_DICTIONARY_TYPE, array('code' => $code));
        return $query->row_array();
    }

    public function get_dictionary_by_id($id) {
        $query = $this->db->get_where(self::TABLE_DICTIONARY_TYPE, array('id' => $id));
        return $query->row_array();
    }

    public function delete_dictionary_type_by_id($id) {
        $this->db->where('id', $id)
            ->delete(self::TABLE_DICTIONARY_TYPE);
        return $this->db->affected_rows() > 0;
    }

    public function get_dictionary_all_types() {
        return $this->db->select('*')
            ->from(self::TABLE_DICTIONARY_TYPE)
            ->order_by("createtime", "ASC")
            -> get() -> result_array();
    }

    public function get_dictionary_items_by_type($type_id) {
        return $this->db->select('*')
            ->from(self::TABLE_DICTIONARY_ITEM)
            -> where("type_id", $type_id)
            ->order_by("display_order", "ASC")
            -> get() -> result_array();
    }

    public function get_dictionary_items($start, $length, $dkey, $search_type, $orderby) {


        if (!empty($dkey)) {
            $this->db->like(self::TABLE_DICTIONARY_ITEM.'.dkey', $dkey);
        }

        if (!empty($search_type)) {
            $this->db->where(self::TABLE_DICTIONARY_ITEM.'.type_id', $search_type);
        }

        $qcount = $this->db
        ->select(self::TABLE_DICTIONARY_ITEM.".*, " . self::TABLE_DICTIONARY_TYPE . ".name as tname")
          ->from(self::TABLE_DICTIONARY_ITEM);



        $db = clone($this->db);
        $count = $qcount ->count_all_results();


        $this->db = $db;
        foreach ($orderby as $okey => $ovalue) {
            $this->db->order_by($okey, $ovalue);
        }

        $this->db->order_by("type_id", "asc");

        $this->db->join(self::TABLE_DICTIONARY_TYPE, self::TABLE_DICTIONARY_TYPE.".id= ".self::TABLE_DICTIONARY_ITEM.".type_id");
        $data = $this->db->limit($length, $start) -> get()-> result_array();
        return array("data"=> $data, "recordsTotal" => $count, "recordsFiltered" => $count);
    }


    public function get_dictionary_item_by_id($id) {
        $query = $this->db->get_where(self::TABLE_DICTIONARY_ITEM, array('id' => $id));
        return $query->row_array();
    }

    public function get_dictionary_item_by_key_typeid($key, $typeid) {
        $query = $this->db->get_where(self::TABLE_DICTIONARY_ITEM, array('type_id' => $typeid, 'dkey'=> $key));
        return $query->row_array();
    }


    public function add_dictionary_item($typeid, $display_order, $name, $dkey, $lang_key, $dvalue, $extra_value, $unit, $version) {
        $data = array(
            'type_id' => $typeid,
            'display_order' => $display_order,
            'name' => $name,
            'dkey' => $dkey,
            'lang_key' => $lang_key,
            'dvalue' => $dvalue,
            'extra_value' => $extra_value,
            'unit'=> $unit,
            'version'=> $version,
            'createtime'=> time()
        );
        $this->db->insert(self::TABLE_DICTIONARY_ITEM, $data);
        return $this->db->affected_rows() > 0;
    }


    public function update_dictionary_item_by_id($id , $typeid, $display_order, $name, $dkey, $lang_key, $dvalue, $extra_value, $unit, $version) {
        $data = array(
            'type_id' => $typeid,
            'display_order' => $display_order,
            'name' => $name,
            'dkey' => $dkey,
            'lang_key' => $lang_key,
            'dvalue' => $dvalue,
            'extra_value' => $extra_value,
            'unit'=> $unit,
            'version'=> $version
        );

        $this->db-> where("id", $id)
            -> update(self::TABLE_DICTIONARY_ITEM, $data);

        return $this->db->affected_rows() > 0;
    }

    public function delete_dictionary_item_by_id($id) {
        $this->db->where('id', $id)
            ->delete(self::TABLE_DICTIONARY_ITEM);
        return $this->db->affected_rows() > 0;
    }

    public function get_current_display_order($typeid) {
        $qcount = $this->db
            ->select("*")
            ->from(self::TABLE_DICTIONARY_ITEM)
         -> where("type_id", $typeid);
        $count = $qcount ->count_all_results();

       // echo $this->db->last_query();
        return $count + 1;
    }

}